package top.minuy.structure.queue.deque;
/**
 * @author Minuy
 * @time 22:11
 * @date 2021/8/22
 */
public class Test {

    public static double testDeque(Deque<Integer> deque, int opCount){
        boolean check = true;
        boolean isFront = false;
        long startTime = System.nanoTime();
        for (int i=0;i<opCount;i++){
            if(isFront){
                deque.addFront(i);
                // System.out.print("在前面入队 " + i + " ");
            }else{
                deque.addLast(i);
                // System.out.print("在后面入队 " + i + " ");
            }
            isFront = !isFront;

            // System.out.println(deque);
        }

        isFront = false;
        int tag = -1;

        for (int i=opCount-1;i>=0;i--){
            if(isFront){
                tag = deque.removeLast();
                // System.out.print("在后面出队 " + tag + " ");
            }else{
                tag = deque.removeFront();
                // System.out.print("在前面出队 " + tag + " ");
            }

            if(tag!=i){
                check = false;
            }

            isFront = !isFront;

            // System.out.println(deque);
        }
        long endTime = System.nanoTime();
        System.out.println("check = "+check+", time = "+(endTime-startTime)/1000000000.0+" s");
        return (endTime-startTime)/1000000000.0;
    }

    public static void main(String[] args) {
        ArrayDeque<Integer> arrayDeque = new ArrayDeque<Integer>();

        System.out.println(arrayDeque);

        for (int i = 0; i < 11; i++) {
            if (i % 2 == 0) {
                arrayDeque.addFront(i);
                System.out.print("在前面入队 " + i + " ");
            } else {
                arrayDeque.addLast(i);
                System.out.print("在后面入队 " + i + " ");
            }

            System.out.println(arrayDeque);
        }

        for (int i = 0; i < 11; i++) {
            if (i % 3 == 0) {
                int r = arrayDeque.removeLast();
                System.out.print("在后面出队 " + r + " ");
            } else {
                int r = arrayDeque.removeFront();
                System.out.print("在前面出队 " + r + " ");
            }

            System.out.println(arrayDeque);
        }


        System.out.println("-----------------Loop Deque----------------");

        LoopDeque<Integer> loopDeque = new LoopDeque<Integer>();

        System.out.println(loopDeque);

        for (int i = 0; i < 11; i++) {
            if (i % 2 == 0) {
                loopDeque.addFront(i);
                System.out.print("在前面入队 " + i + " ");
            } else {
                loopDeque.addLast(i);
                System.out.print("在后面入队 " + i + " ");
            }

            System.out.println(loopDeque);
        }

        for (int i = 0; i < 11; i++) {
            if (i % 2 != 0) {
                int r = loopDeque.removeLast();
                System.out.print("在后面出队 " + r + " ");
            } else {
                int r = loopDeque.removeFront();
                System.out.print("在前面出队 " + r + " ");
            }

            System.out.println(loopDeque);
        }

        int opCount = 100000;
        System.out.println("ArrayDeque");
        testDeque(new ArrayDeque<>(),opCount);
        System.out.println("LoopDeque");
        testDeque(new LoopDeque<>(),opCount);
    }
}
